/*
 * @Description:
 * @Version: 2.0
 * @Autor: zhangjx
 * @Date: 2023-05-27 09:33:56
 * @LastEditors: uphldjx 1373577124@qq.com
 * @LastEditTime: 2023-09-25 09:49:53
 */
import { defineStore } from 'pinia'
import apiMod from '@/apiMod/index'

export const useCrudStore = defineStore('crud', () => {
	const tableLoading = ref(true)
	const nowDict = ref({})
	const menuTreeData = ref([])
	const areaTreeData = ref([])
	const setTableLoading = (data) =>{
		tableLoading.value = data
	}
	const setMenuTreeData = (data) => {
		menuTreeData.value = data
	}
	const setAreaTreeData = (data) => {
		areaTreeData.value = data
	}
	const saveDictForm = async (form, isApi: boolean) => {
		let apiname = form.id ? 'sys_dict_edit' : 'sys_dict_add'
		try {
			let dictId = form.id
			if (isApi) {
				const { data } = await apiMod[apiname](form)
				dictId = form.id ? form.id : data
			}
			const query = {
				dictId: dictId,
				identifying: form.identifying,
			}
			nowDict.value = query
			return Promise.resolve(query)
		} catch (error) {
			return Promise.reject(error)
		}
	}
	return {
		areaTreeData,
		menuTreeData,
		nowDict,
		tableLoading,
		setAreaTreeData,
		setMenuTreeData,
		saveDictForm,
		setTableLoading
	}
})
